/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.cacheplugins.ws  
 */
package com.apache.bid.service.cacheplugins.ws;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.common.vo.BidItemCacheVo;
import com.apache.bid.entity.BidApply;
import com.apache.bid.entity.BidItem;
import com.apache.bid.entity.BidItemPrice;
import com.apache.bid.entity.BidSession;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;

/**
 * description:  同步数据缓存插件
 * @author dyhou 创建时间：2016-7-8  
 */
public class SynicObjeCachePluginImpl implements PluginConnector {

	private IDao bidSessionDao;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		String seId = String.valueOf(param.getParams("seid"));
		String itemIds = String.valueOf(param.getParams("itemIds"));
		String applyids = String.valueOf(param.getParams("applyids"));
		ResultEntity entity = new ResultEntity();
		if (Validator.isNull(seId) && Validator.isNull(itemIds) && Validator.isNull(applyids)) {
			entity.setMessage("缺少必要参数");
		} else {
			String msg = "";
			if (Validator.isNotNull(seId)) {
				msg = synSessionInfo(seId);
			} else if (Validator.isNotNull(itemIds)) {
				msg = synItemInfo(itemIds);
			} else if (Validator.isNotNull(applyids)) {
				msg = synApplyInfo(applyids);
			}
			entity.setMessage(msg);
		}
		entity.setResult("true");
		return entity;
	}

	private String synSessionInfo(String sessionId) {
		MethodParam param = new MethodParam("ById", "", "", BidSession.class.getName());
		param.setInfoId(sessionId);
		BidSession sessionVo = (BidSession) bidSessionDao.selectById(param);
		if (!Validator.isEmpty(sessionVo)) {
			BidCacheFactory.getInstance().getCacheManager("session").createCacheObject(sessionVo);
			return "活动同步成功";
		}
		return "没有获取到[" + sessionId + "]活动";
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private String synItemInfo(String itemIds) {
		String[] str = itemIds.split(",");
		BidCacheManager itemCache = BidCacheFactory.getInstance().getCacheManager("item");
		BidCacheManager itemPriceCache = BidCacheFactory.getInstance().getCacheManager("itemPrice");
		for (int i = 0; i < str.length; i++) {
			MethodParam param = new MethodParam("ByIdVo", "", "", BidItem.class.getName());
			param.setInfoId(str[i]);
			BidItemCacheVo cacheVo = (BidItemCacheVo) bidSessionDao.selectById(param);
			if (!Validator.isEmpty(cacheVo)) {
				itemCache.createCacheObject(cacheVo);
				param.setKey("ById");
				param.setSpanceName(BidItemPrice.class.getName());
				BidItemPrice price = (BidItemPrice) bidSessionDao.selectById(param);
				if (!Validator.isEmpty(price)) {
					itemPriceCache.createCacheObject(price);
				}
			}
		}
		return "同步标的成功";
	}

	private String synApplyInfo(String applyIds) {
		String[] str = applyIds.split(",");
		BidCacheManager applyCache = BidCacheFactory.getInstance().getCacheManager("apply");
		for (int i = 0; i < str.length; i++) {
			MethodParam param = new MethodParam("ById", "", "", BidApply.class.getName());
			param.setInfoId(str[i]);
			BidApply apply = (BidApply) bidSessionDao.selectById(param);
			if (!Validator.isEmpty(apply)) {
				applyCache.createCacheObject(apply);
			}
		}
		return "同步竞买人成功";
	}

	public void setBidSessionDao(IDao bidSessionDao) {
		this.bidSessionDao = bidSessionDao;
	}

}
